package com.gao.modules.user.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gao.base.Result;
import com.gao.modules.user.dto.SysUserDto;
import com.gao.modules.user.pojo.SysUser;
import com.gao.modules.user.service.SysUserService;
import com.gao.modules.user.vo.SysUserVo;
import com.gao.modules.userdepart.pojo.SysUserDepart;
import com.gao.modules.userrole.pojo.SysUserRole;
import com.gao.utils.SysUserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private SysUserService sysUserService;

    @ResponseBody
    @RequestMapping("/test")
    public String test(){
        return "123";
    }

    @RequestMapping("/queryAllUserDetail")
    public List queryAllUserDetail(){
        List<Map<String, Object>> allUser = sysUserService.listMaps();
        return allUser;
    }

    @GetMapping("/queryUserByDepart")
    public IPage<SysUserVo> queryUserByDepart(@RequestParam String departName,
                                              @RequestParam(name = "pageNo", defaultValue = "0") Integer pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){

        Page page = new Page(pageNo, pageSize);
        IPage<SysUserVo> sysUserVoIPage = sysUserService.queryDepartAllUser(departName,page);
        return sysUserVoIPage;

    }

    @PostMapping("/insertOneUser")
    public Result<String> insertOneUser(@RequestBody SysUserDto sysUserDto){


        SysUserDepart sysUserDepart = new SysUserDepart();
        SysUserRole sysUserRole = new SysUserRole();
        SysUser sysUser = SysUserUtil.UserDtoToPojo(sysUserDto,sysUserDepart,sysUserRole);
        return sysUserService.insert(sysUser,sysUserDepart,sysUserRole);
    }

    @PostMapping("/deleteOneUserById")
    public Result<String> deleteOneUser(@RequestParam String userId){

        return sysUserService.deleteUserById(userId);
    }

    @PostMapping("/updateOneUserById")
    public Result<String> updateOneUserById(@RequestBody SysUserDto sysUserDto){
        SysUserDepart sysUserDepart = new SysUserDepart();
        SysUserRole sysUserRole = new SysUserRole();
        SysUser sysUser = SysUserUtil.UserDtoToPojo(sysUserDto,sysUserDepart,sysUserRole);
        return sysUserService.updateUserById(sysUser,sysUserDepart,sysUserRole);
    }

    @GetMapping("/sendMail")
    public void sendMail(){
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom("1181810576@qq.com");
        simpleMailMessage.setTo("augustin_An@126.com");
        simpleMailMessage.setSubject("测试邮件");
        simpleMailMessage.setText("测试邮件-->  链接为： https://www.baidu.com");
        javaMailSender.send(simpleMailMessage);
    }


}
